using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using Impact.Infrastructure.Configuration;
using Impact.Infrastructure;
using Impact.Web.Foundation;
using Impact.Model.Objects;
using StructureMap;
using Impact.Model.Service;

namespace Impact.Web.Admin
{
    /// <summary>
    /// code-behind for the site management page of the admin
    /// </summary>
    public partial class Site : PageBase
    {

        /// <summary>
        /// event handler fired when the user clicks submit
        /// </summary>
        protected void SubmitCommand(object sender, EventArgs e)
        {
            SaveData();
            Bind();
        }

        /// <summary>
        /// event handler fired when the user clicks cancel
        /// </summary>
        protected void CancelCommand(object sender, EventArgs e)
        {
            // reload to get rid of any changes.
            Response.Redirect(Request.Url.ToString());
        }

        /// <summary>
        /// override of bind to perform specific form setup operations and bindings
        /// </summary>
        protected override void Bind()
        {
            base.Bind();

            IPageService pageService = ObjectFactory.GetInstance<IPageService>();
            ImpactPageCollection pages = pageService.FetchAllPages(false);
            this.defaultPage.DataSource = pages;
            this.defaultPage.DataTextField = ImpactPage.Columns.PageKey;
            this.defaultPage.DataValueField = ImpactPage.Columns.PageKey;
            this.defaultPage.DataBind();

            ImpactSettings appSettings = ObjectFactory.GetInstance<IConfigProvider>().Settings;

            this.defaultPage.SelectedValue = appSettings.DefaultPage;
            this.pageTitlePrefix.Text = appSettings.PageTitlePrefix;
            this.footerText.Text = appSettings.FooterText;
            this.webmasterEmail.Text = appSettings.WebmasterEmail;

        }

        private void SaveData()
        {

            ImpactSettings appSettings = new ImpactSettings();
            appSettings.DefaultPage = this.defaultPage.SelectedValue;
            appSettings.PageTitlePrefix = this.pageTitlePrefix.Text;
            appSettings.FooterText = this.footerText.Text;
            appSettings.WebmasterEmail = this.webmasterEmail.Text;

            ObjectFactory.GetInstance<IConfigProvider>().Settings = appSettings;

        }


    }
}
